<!--  -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="商品名称">
                            <a-input v-model="goodsName" allow-clear placeholder="请输入商品名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="商品类型">
                            <a-select v-model="specialCateId" show-search placeholder="请选择类型" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in classifyList" :key="i" :value="item.id">{{item.cateName}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="12">
                        <a-form-item label="供应商">
                            <a-select v-model="makeId" show-search placeholder="请选择供应商" allowClear @change="getData">
                                <a-select-option v-for="(item) in makeList" :key="item.id + item.makeName" :value="item.id">{{item.makeName}}</a-select-option>
                            </a-select>
                            <!-- <a-input v-model="supplier" allow-clear placeholder="请输入供应商名称" /> -->
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="商品状态">
                            <a-select v-model="status" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="query()">查询</a-button>
                            <a-button icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table size="small" rowKey="id" :loading="isload" @change="handleTableChange" :columns="columns" :data-source="dataList" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}"  :scroll="{ x: 1300 }"  >
            <a-avatar  slot="defaultPicture" shape="square"  slot-scope="defaultPicture" :src="defaultPicture" />
           <div  slot="isGeneralMall" slot-scope="isGeneralMall" >
                 <a-tag :color="isGeneralMall==1?'#87d068':'#666'">
                     {{isGeneralMall==1?'是':'否'}}
                 </a-tag>
            </div>
           <div  slot="isGroupWork" slot-scope="text, record" >
                 <a-tag :color="record.isGroupWork==1?'#87d068':'#666'">
                     {{record.isGroupWork==1?'是':'否'}}
                 </a-tag>
                <div>拼团价格:{{record.groupPrice}}元</div> 
                <div>单次购买数量:{{record.groupBuyQuantityLimit}}次</div>
            </div>
            <div  slot="isRecommend" slot-scope="isRecommend" >
                 <a-tag :color="isRecommend==1?'#87d068':'#666'">
                     {{isRecommend==1?'是':'否'}}
                 </a-tag>
            </div>
              <div  slot="isMoreSpec" slot-scope="isMoreSpec" >
                 <a-tag :color="isMoreSpec==1?'#87d068':'#666'">
                     {{isMoreSpec==1?'是':'否'}}
                 </a-tag>
            </div>
             <div  slot="isNewPeople" slot-scope="text, record" >
                 <a-tag :color="record.isNewPeople==1?'#87d068':'#666'">
                     {{record.isNewPeople==1?'是':'否'}}
                 </a-tag>
                <div>新人专享价格:{{record.newPeoplePrice}}元</div> 
            </div>
              <div slot="status" slot-scope="status">
                <a-switch checked-children="上架" un-checked-children="下架" disabled :checked="status == 1 ? true : false" />
            </div>
            <!-- <div slot="sort" slot-scope="text, record">
                <div>普通商城排序：{{record.sort}}</div>
                <div>拼团商城排序：{{record.groupSort}}</div>
                <div>新人专享排序：{{record.newPeopleSort}}</div>
            </div> -->
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>
        <!-- 添加、修改、删除个人推送消息 -->
        <a-modal
            title="添加、修改个人推送消息"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="用户ID">
                        <a-input v-model="item.userId" type="number" :disabled="!isAdd" placeholder="请输入用户ID" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="消息标题">
                        <a-input v-model="item.title" placeholder="请输入消息标题" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="消息内容">
                        <a-input v-model="item.message" type="textarea" placeholder="请输入消息内容" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryGoodsInfo , updateGoodsInfo } from '@/api/mall/info';
import { queryMake } from '@/api/mall/make';
import { queryBrand }  from '@/api/brand';
import {  querySpecialClassify  } from '@/api/mall/classify';
export default {
    data () {
        return {
            userId: undefined,
            brandId: this.global.brandId,
            isload: false,
            dataList: [],
            size: 10,
            page: 1,
            total: 0,
            columns,
            visible: false,
            confirmLoading: false,
            item: {
                id: undefined,
                userId: '',
                title: '',
                message: '',
            },
            goodsTypeList:[
                {key:"isGeneralMall",value:'普通商城'},
                {key:"isGroupWork",value:'拼团'},
                {key:"isNewPeople",value:'新人专享'}
            ],
            goodsType:[],
            goodsName:"",
            makeId: undefined,
            makeList: [],
            isAdd: true,
            specialCateId: undefined,
            classifyList: [],
            status: undefined,
            statusList: [ { id: 0, name: '下架'},{ id: 1, name: '上架'},],
        };
    },
    async created(){
        this._getMake();
        this.getClassify();
        this.getData()
    },
    methods: {
        getClassify(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'page':0,
                'size':99,
            }
            querySpecialClassify(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.classifyList = res.data.content  
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        query(){
            this.page=1
            this.getData()
        },
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'goodsName':_this.goodsName,
                'makeId': _this.makeId,
                'specialCateId': _this.specialCateId,
                'status': _this.status,
                'size':_this.size,
                'page':_this.page - 1,
            }
            this.goodsType.forEach(item => {
                params[item]='1'
            });
            queryGoodsInfo(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true
            this.$router.push({ path: '/mall/goods/detail',query:{id:'',brandId:this.brandId }})
        },
        // 修改
        update(item){
            this.isAdd = false
            this.item = JSON.parse(JSON.stringify(item))
            this.$router.push({ path: '/mall/goods/detail',query:{id:item.id,brandId:this.item.brandId }})
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            if(_this.item.userId == ''){
                _this.$message.warning('请输入接受消息的用户ID！');
                return
            }
            if(_this.item.title == ''){
                _this.$message.warning('请输入推送消息的标题！');
                return
            }
            if(_this.item.message == ''){
                _this.$message.warning('请输入推送消息的内容！');
                return
            }
            const params = {
                'brandId':_this.brandId,
                'userId':_this.item.userId,
                'id':_this.item.id,
                'title':_this.item.title,
                'message':_this.item.message,
            }
            updatePushUserMessage(params)
            .then(res=>{
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                    _this.getData()
                    _this.visible = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除消息
        del(item){
            let _this = this
             var formData = new FormData()
                formData.append('id',item.id)
                formData.append('isDelete',1)
            updateGoodsInfo(formData)
            .then(res=>{
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },// 获取品牌商列表
        _getMake(){
            let _this = this
            let page = 0
            const fn = function(){
                const params = {
                    'brandId':_this.brandId,
                    'size':100,
                    'page':page,
                }
                queryMake(params)
                .then(res=>{
                    if(res.success){
                        if(page == 0){
                            _this.makeList = res.data.content
                        }else{
                            _this.makeList = [..._this.makeList,...res.data.content]
                        }
                        if((res.data.totalElements > (page + 1) * 99)){
                            page++;
                            fn()
                        }
                    }else{
                        _this.$notification['error']({
                            message: '提示',
                            description: res.errMessage,
                            duration: 8,
                        })
                    }
                })
            }
            fn()
        },
    }
}

</script>
<style scoped>

</style>